#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
int main()
{
    pid_t id = fork();
    if(id == 0)
    {
        printf("I am child....mypid:%d fatherPid:%d\n",getpid(),getppid());
        char* argv[] = {
            "ls","-a","-l",NULL
        };
        //执行程序替换
        //execl("/usr/bin/ls","ls","-a","-l",NULL);
        //execvp("ls",argv);
        //execlp("ls","ls","-a","-l",NULL);
        //execv("/usr/bin/ls",argv);
        exit(1);//程序替换执行成功是不返回的
    }
    else if(id < 0)
    {
        perror("fork:");
        exit(2);
    }
    //parent
    int status = 0;
    int ret = waitpid(-1,&status,0);//waitpid等待成功返回子进程的pid
    if(ret > 0)
    {
        printf("father wait childPid:%d success\n",ret);
        printf("exitCode:%d,exitSignal:%d,core dump yes or no:%d\n", \
         (status >> 8)&0xff,status&0x7f,(status>>7)&1);
    }
    return 0;
}